const {query} = require('../models/db');

let goodsCache = null;
module.exports.initLocals = async (req, res, next) => {
    // 全局数据可放至res.locals中
    if (goodsCache) {
        res.locals.goods = goodsCache;
        next();
    } else {
        const sql = 'SELECT * FROM sure.goods;';
        try {
            const Goods = await query(sql);
            Goods.forEach(goods => goods.img = goods.img.split(','));
            goodsCache = res.locals.goods = Goods;
            next();
        } catch (e) {
            next(e);
        }
    }
};
